package com.powernode.controller;

import com.powernode.domain.SysFolder;
import com.powernode.domain.SysUser;
import com.powernode.service.SysFolderService;
import com.powernode.utils.WebUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * 描述：文件夹管理
 * 作者：动力节点·武汉
 */
@Controller
@RequestMapping("folder")
public class FolderController {

    @Resource
    private SysFolderService folderService;


    /**
     * 根据当前用户查询文件夹并跳转到templates/folder/list.html
     */
    @GetMapping("queryFolderList")
    public String queryFolderList(String folderName, Model model){
        //解决模糊查询之后丢失关键字的问题
        model.addAttribute("folderName",folderName);
        //得到当前用户对象
        Integer userId= WebUtils.getCurrentUser().getUserId();
        List<SysFolder> folderList=folderService.queryFolderList(folderName,userId);
        model.addAttribute("folderList",folderList);
        return "folder/list";
    }

    /**
     * 跳转到添加页面
     */
    @GetMapping("toAdd")
    public String toAdd(){
        return "folder/add";
    }
    /**
     * 添加
     */
    @PostMapping("add")
    public String add(SysFolder sysFolder){
        sysFolder.setUserId(WebUtils.getCurrentUser().getUserId());
        this.folderService.insertSelective(sysFolder);
        return "redirect:/folder/queryFolderList";
    }

    /**
     * 跳转到修改页面
     */
    @GetMapping("toUpdate")
    public String toUpdate(Integer folderId,Model model){
        //根据folderId查询folder
        SysFolder folder = this.folderService.selectByPrimaryKey(folderId);
        model.addAttribute("folder",folder);
        return "folder/update";
    }
    /**
     * 修改
     */
    @PostMapping("update")
    public String update(SysFolder sysFolder){
        sysFolder.setUserId(WebUtils.getCurrentUser().getUserId());
        this.folderService.updateByPrimaryKeySelective(sysFolder);
        return "redirect:/folder/queryFolderList";
    }
    /**
     * 删除
     */
    @GetMapping("delete")
    public String delete(Integer folderId){
        this.folderService.deleteByPrimaryKey(folderId);
        return "redirect:/folder/queryFolderList";
    }

}
